﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Golos.BackEnd.Client;
using Golos.Polls.PollCache;

namespace Golos.Web.handlers
{
    public class PollsHelper: HandlerBase
    {
        public PollsHelper(HttpContext httpContext)
            : base(httpContext)
        {
        }

        public override void Handle()
        {
        }

        public static string GetPollInfo(Guid qid)
        {
            // TEMPLATE
            // {
            //   "q quid": 
            //   [
            //     "q title", 
            //     "end date",
            //     "tag1;tag2;tag3",
            //     null,
            //     {
            //         "a1 quid":["answer title", null],
            //         "a2 quid":["answer title", null],
            //         "a3 quid":["answer title", null]
            //     }
            //   ]
            // }

            string json = "";

            PollInfo pollInfo = PollsClient.GetPollInfo(qid);
            
            json += "\"" + pollInfo.Id + "\": [";
            json += "\"" + pollInfo.QuestionText + "\",";
            json += "\"" + String.Format("{0:dd/MM/yyyy}", pollInfo.EndDate) + "\",";

            List<string> tags = TagsClient.GetQuestionTags(qid);

            json += "\"";
            foreach (string tag in tags)
            {
                json += tag + ";";
            }
            json += "\",";

            json += "null, {";

            Dictionary<int, Answer> id = new Dictionary<int, Answer>();
            
            foreach (KeyValuePair<Guid, Answer> keyValuePair in pollInfo.Answers)
            {
                id.Add(keyValuePair.Value.OrderNumber, keyValuePair.Value);
            }

            Dictionary<int, Answer> sortedDict = (from entry in id orderby entry.Key ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);

            foreach (KeyValuePair<int, Answer> keyValuePair in sortedDict)
            {
                json += "\"" + keyValuePair.Value.Id + "\": [\"" + keyValuePair.Value.AnswerText + "\", null],";
            }

            json = json.TrimEnd(new char[] {',', ' '});
            json += "} ],";

            return json;
        }

        public static string GetVotedPollInfo(Guid userId, Guid questionId)
        {
            // TEMPLATE
            // {
            //   "q quid": 
            //   [
            //     "q title", 
            //     "end date",
            //     "tag1;tag2;tag3",
            //     "quid of my chosen answer", 
            //     {
            //         "a1 quid":["answer title", count],
            //         "a2 quid":["answer title", count],
            //         "a3 quid":["answer title", count]
            //     }
            //   ]
            // }

            string json = "";

            PollInfo pollInfo = PollsClient.GetPollInfo(questionId);

            Dictionary<int, Answer> id = new Dictionary<int, Answer>();
            
            foreach (KeyValuePair<Guid, Answer> keyValuePair in pollInfo.Answers)
            {
                id.Add(keyValuePair.Value.OrderNumber, keyValuePair.Value);
            }

            Dictionary<int, Answer> sortedDict = (from entry in id orderby entry.Key ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
            
            json += "\"" + pollInfo.Id + "\": [";
            json += "\"" + pollInfo.QuestionText + "\",";
            json += "\"" + String.Format("{0:dd/MM/yyyy}", pollInfo.EndDate) + "\",";

            List<string> tags = TagsClient.GetQuestionTags(questionId);

            json += "\"";
            foreach (string tag in tags)
            {
                json += tag + ";";
            }
            json += "\",";

            UserAnswerInfo userAnswerInfo = PollsClient.GetUserAnswerInfo(questionId, userId);

            if (userAnswerInfo != null)
            {
                json += "\"" + userAnswerInfo.AnswerId + "\", {";

                Dictionary<Guid, int> qCount = PollsClient.GetVoicesCount(questionId);

                foreach (KeyValuePair<int, Answer> keyValuePair in sortedDict)
                {
                    if (qCount.ContainsKey(keyValuePair.Value.Id))
                    {
                        json += "\"" + keyValuePair.Value.Id + "\": [\"" + keyValuePair.Value.AnswerText + "\", " + qCount[keyValuePair.Value.Id] + "],";
                    }
                    else
                    {
                        json += "\"" + keyValuePair.Value.Id + "\": [\"" + keyValuePair.Value.AnswerText + "\", 0 ],";
                    }
                }
            }
            else
            {
                json += "null, {";

                foreach (KeyValuePair<int, Answer> keyValuePair in sortedDict)
                {
                    json += "\"" + keyValuePair.Value.Id + "\": [\"" + keyValuePair.Value.AnswerText + "\", null],";
                }
            }
            json = json.TrimEnd(new char[] { ',', ' ' });
            json += "} ],";

            return json;
        }
    }
}